package com.zoer.commons.presentation;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport{
	HttpServletRequest myRequest;
	HttpServletResponse myResponse;
	HttpSession mySession;
	public String execute() throws Exception {
		this.myRequest = ServletActionContext.getRequest();
		this.myResponse = ServletActionContext.getResponse();
		this.mySession = myRequest.getSession();
		String uri = ServletActionContext.getRequest().getRequestURI();
		String[] uriPart = uri.split("\\.");
		String method;
		if (uriPart.length == 3) {
			method = uriPart[1];
		} else {
			method = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf(".action"));
		}
		return exec(method);
	}
	@SuppressWarnings("unchecked")
	public String exec(String methodStr) {
		String result = null;
		try {
			Class[] parameterTypes = new Class[]{};
			Method method = this.getClass().getMethod(methodStr, parameterTypes);
			result = (String) method.invoke(this, new Object[]{});
		} catch (Exception e) {
			e.printStackTrace(); 
		}
		
		return result;
	}
	protected ApplicationContext getApplicationContext() {
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());  
		return ctx;
	}
	public Object getBean(String beanName) {
		ApplicationContext context = getApplicationContext();
		return context.getBean(beanName);
	}
}
